package com.app.couponsharelife.ui.main.nativelife.fragment

import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.app.couponsharelife.R
import com.app.couponsharelife.base.LazyFragment
import com.scwang.smartrefresh.layout.api.RefreshFooter
import com.scwang.smartrefresh.layout.api.RefreshHeader
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.constant.RefreshState
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener
import kotlinx.android.synthetic.main.base_refresh_head.*
import kotlinx.android.synthetic.main.fragment_lifereferrals.*
import kotlinx.android.synthetic.main.lifereferrals_rvtopitem.view.*
import android.content.Context
import android.content.Intent
import android.text.Html
import android.widget.TextView
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.retrofit.RetrofitWorker
import com.app.couponsharelife.data.remote.retrofit.exception.OtherException
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.manage.Contacts.REMOTE_IMAGE_URL
import com.app.couponsharelife.manage.Contacts.TCP
import com.app.couponsharelife.ui.main.home.shopweb.ShopWebActivity
import com.app.couponsharelife.ui.main.nativelife.activity.LifeClassListActivity
import com.app.couponsharelife.ui.main.nativelife.activity.NativeLifeDetailsActivity
import com.app.couponsharelife.ui.view.*
import com.app.couponsharelife.utils.AppUtils
import com.app.couponsharelife.utils.CardConfig
import com.app.couponsharelife.utils.ToastUtil
import com.tencent.mm.opensdk.modelbiz.AddCardToWXCardPackage
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.lifereferrals_couponrvitm.view.*
import kotlinx.android.synthetic.main.lifereferrals_rvitem.view.*
import kotlinx.android.synthetic.main.lifereferrals_shoprvitm.view.*
import kotlinx.android.synthetic.main.recylerview_item.view.*
import java.text.DecimalFormat
import java.util.*

/**
 *Created by xct on 2019/1/5
 *Description:本地生活推荐页面
 */
class LifeReferralsFragment : LazyFragment() {
    private val decimalFormat = DecimalFormat("#.##")
    private val TYPETOP = 0
    private val TYPEITM = 2
    private var bannerView: Banner? = null
    private var mAdapter: MyAdapter? = null
    private var myCouponAdapter: MyCouponAdapter? = null
    private var mTradingArea: TradingArea? = null
    private var cityName = ""
    private var longitude = ""
    private var latitude = ""
    private var mCardList = arrayListOf<Any>()
    private var mCardList1 = arrayListOf<Any>()
    private var bannerList = arrayListOf<LifeTopBanner>()
    private var classList = arrayListOf<Classify>()
    private var centerBannerList = arrayListOf<CenterBanner>()
    private var shopList = arrayListOf<LifeShopRecord>()
    private var cardList = arrayListOf<LifeWXCardBean>()
    private var mShopAdaptper: MyShopCardAdapter? = null
    private var mShopAdaptper1: MyShopCardAdapter1? = null
    private var pageIndex = 1
    private var userID = ""
    private var isFirst = true

    companion object {
        fun newInstance(tradingArea: TradingArea): LifeReferralsFragment {
            val fragment = LifeReferralsFragment()
            val bundle = Bundle()
            bundle.putParcelable("Data", tradingArea)
            fragment.arguments = bundle
            return fragment
        }
    }

    override val viewLayoutId: Int
        get() = R.layout.fragment_lifereferrals

    override fun initViews() {
        mTradingArea = arguments!!.getParcelable("Data")
        cityName = AppUtils.getString("CityName", "杭州")
        longitude = AppUtils.getString("Longitude", Contacts.HZ_CENTER_LONGITUDE)
        latitude = AppUtils.getString("Latitude", Contacts.HZ_CENTER_LATITUDE)
        userID = AppUtils.getString(Contacts.USERID, "")
        bannerList = (parentFragment as NativeLifeFragment).bannerList
        centerBannerList = (parentFragment as NativeLifeFragment).centerBannerList
        classList = (parentFragment as NativeLifeFragment).classList
        setRefresh()
        lifereferrals_rv.layoutManager = LinearLayoutManager(activity)
        mAdapter = MyAdapter()
        lifereferrals_rv.adapter = mAdapter
        getShopPager(false, "8")
        getShopPager1("9")
        getCardList()
        getShopList()
    }

    private fun setRefresh() {
        hotsale_refreshimg.setImageResource(R.drawable.anim_refresh)
        val animationDrawable = hotsale_refreshimg.drawable as AnimationDrawable
        lifereferrals_smartrefresh.setOnMultiPurposeListener(object : OnMultiPurposeListener {
            override fun onFooterMoving(footer: RefreshFooter?, isDragging: Boolean, percent: Float, offset: Int, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onHeaderStartAnimator(header: RefreshHeader?, headerHeight: Int, maxDragHeight: Int) {

            }

            override fun onFooterReleased(footer: RefreshFooter?, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onStateChanged(refreshLayout: RefreshLayout, oldState: RefreshState, newState: RefreshState) {

            }

            override fun onHeaderMoving(header: RefreshHeader?, isDragging: Boolean, percent: Float, offset: Int, headerHeight: Int, maxDragHeight: Int) {
                setAnima(percent)
            }

            override fun onFooterFinish(footer: RefreshFooter?, success: Boolean) {

            }

            override fun onFooterStartAnimator(footer: RefreshFooter?, footerHeight: Int, maxDragHeight: Int) {

            }

            override fun onHeaderReleased(header: RefreshHeader?, headerHeight: Int, maxDragHeight: Int) {
                animationDrawable.start()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                getShopList()
                lifereferrals_smartrefresh.finishLoadMore(2000)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageIndex = 1
                getShopPager(true, "8")
                getShopPager1("9")
                getCardList()
                getShopList()
            }

            override fun onHeaderFinish(header: RefreshHeader?, success: Boolean) {
                hotsale_refreshimg.setImageResource(R.drawable.refresh_0)
                animationDrawable.stop()
            }
        })
    }

    private fun setAnima(vlaue: Float) {
        val layoutParams = hotsale_refreshimg.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.matchConstraintPercentHeight = vlaue / 2
        layoutParams.dimensionRatio = "0.875:1"
        hotsale_refreshimg.layoutParams = layoutParams
    }

    override fun lazyLoad() {

    }

    private fun wxCard(cardid: String, ext: String) {
        val item = AddCardToWXCardPackage.WXCardItem()
        val cardItems = arrayListOf<AddCardToWXCardPackage.WXCardItem>()
        val req = AddCardToWXCardPackage.Req()
        val api = WXAPIFactory.createWXAPI(activity, Contacts.WECHAT_APP_ID, false)
        item.cardId = cardid
        item.cardExtMsg = ext
        cardItems.add(item)
        req.cardArrary = cardItems
        api.sendReq(req)
    }

    private fun setShopIntent(shopID: String) {  //跳转店铺详情页面
        if (shopID.isEmpty()) {
            ToastUtil.getInstance().showToast("数据出错请稍后再试！")
        } else {
            val intent = Intent(activity, NativeLifeDetailsActivity::class.java)
            intent.putExtra("ShopID", shopID)
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        if (!isFirst) {
            getCardList()
        }
        isFirst = false
    }

    private fun loadImag(imageView: ImageView, url: String, errorimag: Int, radius: Int) {
        var imgUrl = if (url != null && url.isNotEmpty()) {
            if (url.startsWith("//")) {
                TCP + url
            } else if (url.startsWith("http")) {
                url
            } else {
                REMOTE_IMAGE_URL + url
            }
        } else {
            url
        }
        if (radius > 20) {
            Glide.with(activity!!).asBitmap()
                    .apply(RequestOptions().circleCrop().error(errorimag))
                    .load(imgUrl)
                    .into(imageView)
        } else {
            Glide.with(activity!!).asBitmap()
                    .apply(RequestOptions().transforms(CenterCrop(), RoundedCorners(radius)).error(errorimag))
                    .load(imgUrl)
                    .into(imageView)
        }
    }

    private fun getShopPager(isRefresh: Boolean, acType: String) {
        if (!isRefresh) {
            showProgressDialog("加载中...")
        }
        val map = HashMap<String, String>()  //30.505484,118.586426
        map["pageNo"] = "1"
        map["pageSize"] = "10"
        map["acType"] = acType
        map["Longitude"] = longitude
        map["Latitude"] = latitude
        map["distance"] = "60"
        RetrofitWorker.retrofitWorker.lifeGetShopPage(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<LifeShopBean>>() {
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<LifeShopBean>) {
                        if (it.code == 200) {
                            if (it.data != null && it.data!!.records != null) {
                                mCardList.clear()
                                mCardList.addAll(it.data!!.records as ArrayList)
                                //mShopAdaptper!!.notifyDataSetChanged()
                            }
                        }
                        if (isRefresh) {
                            lifereferrals_smartrefresh.finishRefresh()
                        } else {
                            dismissProgressDialog()
                        }
                    }

                    override fun onError(e: Throwable) {
                        if (isRefresh) {
                            lifereferrals_smartrefresh.finishRefresh()
                        } else {
                            dismissProgressDialog()
                        }
                        if (e is OtherException) {
                            showToastMsg(e.msg)
                        } else {
                            showToastMsg("网络加载出错了！")
                        }
                    }
                })
    }

    private fun getShopPager1(acType: String) {
        val map = HashMap<String, String>()
        map["pageNo"] = "1"
        map["pageSize"] = "10"
        map["acType"] = acType
        map["Longitude"] = longitude
        map["Latitude"] = latitude
        map["distance"] = "60"
        RetrofitWorker.retrofitWorker.lifeGetShopPage(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<LifeShopBean>>() {
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<LifeShopBean>) {
                        if (it.code == 200) {
                            if (it.data != null && it.data!!.records != null) {
                                mCardList1.clear()
                                mCardList1.addAll(it.data!!.records as ArrayList)
                                //mShopAdaptper1!!.notifyDataSetChanged()
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        if (e is OtherException) {
                            showToastMsg(e.msg)
                        } else {
                            showToastMsg("网络加载出错了！")
                        }
                    }
                })
    }

    private fun getShopList() {
        val map = HashMap<String, String>()
        map["pageNo"] = pageIndex.toString()
        map["pageSize"] = "10"
        map["Longitude"] = longitude
        map["Latitude"] = latitude
        map["distance"] = "60"
        RetrofitWorker.retrofitWorker.lifeGetShopPage(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<LifeShopBean>>() {
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<LifeShopBean>) {
                        if (it.code == 200) {
                            if (it.data != null && it.data!!.records != null && it.data!!.records!!.isNotEmpty()) {
                                if (pageIndex == 1) {
                                    shopList.clear()
                                }
                                shopList.addAll(it.data!!.records as ArrayList)
                                if (shopList.size >= it.data!!.total!!) {
                                    lifereferrals_smartrefresh.finishLoadMoreWithNoMoreData()
                                } else {
                                    if (pageIndex > 1) {
                                        lifereferrals_smartrefresh.finishLoadMore()
                                    }
                                }
                                pageIndex++
                                mAdapter!!.notifyDataSetChanged()
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        if (pageIndex > 1) {
                            lifereferrals_smartrefresh.finishLoadMore()
                        }
                        if (e is OtherException) {
                            showToastMsg(e.msg)
                        } else {
                            showToastMsg("网络加载出错了！")
                        }
                    }
                })
    }

    private fun getCardList() {
        val map = HashMap<String, String>()
        map["sellerId"] = userID
        map["cityName"] = cityName
        map["Longitude"] = longitude
        map["Latitude"] = latitude
        RetrofitWorker.retrofitWorker.getWxCardList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<RemoteResultBean<List<LifeWXCardBean>>>() {
                    override fun onComplete() {

                    }

                    override fun onNext(it: RemoteResultBean<List<LifeWXCardBean>>) {
                        if (it.code == 200) {
                            if (it.data != null && it.data!!.isNotEmpty()) {
                                cardList.clear()
                                cardList.addAll(it.data!!)
                                myCouponAdapter!!.notifyDataSetChanged()
                            }
                        }
                    }

                    override fun onError(e: Throwable) {
                        if (e is OtherException) {
                            showToastMsg(e.msg)
                        } else {
                            showToastMsg("网络加载出错了！")
                        }
                    }
                })
    }

    private fun getWXExt(cardid: String, wxId: String) {
        showProgressDialog("请稍后...")
        val map = HashMap<String, String>()
        map["wxid"] = wxId
        map["cardid"] = cardid
        map["userId"] = userID
        RetrofitWorker.retrofitWorker.getWxCardExt(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DisposableObserver<WXExtBean>() {
                    override fun onComplete() {

                    }

                    override fun onNext(it: WXExtBean) {
                        dismissProgressDialog()
                        wxCard(it.cardid!!, it.cardext!!)
                    }

                    override fun onError(e: Throwable) {
                        dismissProgressDialog()
                        if (e is OtherException) {
                            showToastMsg(e.msg)
                        } else {
                            showToastMsg("网络加载出错了！")
                        }
                    }

                })
    }

    inner class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return if (TYPETOP == viewType) {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.lifereferrals_rvtopitem, parent, false)
                MyTopHolder(view)
            } else {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.lifereferrals_rvitem, parent, false)
                MyHolder(view)
            }
        }

        override fun getItemCount(): Int {
            return shopList.size + 1
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (holder is MyTopHolder) {
                bannerView!!.setImageLoader(GlideImageLoader())
                        .setPageTransformer(false, ScaleTransformer())
                        .setOffscreenPageLimit(3)
                        .setImages(bannerList)
                        .setBannerStyle(BannerConfig.NOT_INDICATOR)
                        .setOnBannerListener {
                            if (bannerList[it].link != null && bannerList[it].link!!.isNotEmpty()) {
                                val intent = Intent(activity, ShopWebActivity::class.java)
                                intent.putExtra("url", bannerList[it].link)
                                startActivity(intent)
                            }
                        }
                        .start()
                holder.shopRv!!.layoutManager = StaggeredGridLayoutManager(2, LinearLayoutManager.HORIZONTAL)
                holder.shopRv!!.adapter = MyShopAdapter()
                holder.shopRv!!.setScrollSpeed(1)
                holder.shopRv!!.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                        val layoutManager = recyclerView.layoutManager as StaggeredGridLayoutManager
                        val firstPositions = IntArray(layoutManager.spanCount)
                        layoutManager.findFirstCompletelyVisibleItemPositions(firstPositions)
                        val firstPosition = firstPositions[0]
                        if (firstPosition == 0) {
                            holder.shopRv!!.setType(true)
                        }
                        val lastPositions = IntArray(layoutManager.spanCount)
                        layoutManager.findLastCompletelyVisibleItemPositions(lastPositions)
                        //layoutManager.findLastVisibleItemPositions()
                        if (layoutManager.itemCount % 2 == 1) {
                            var lastPosition = lastPositions[0]
                            if (lastPosition == layoutManager.itemCount - 1) {
                                holder.shopRv!!.setType(false)
                            }
                        } else {
                            var lastPosition = lastPositions[1]
                            if (lastPosition == layoutManager.itemCount - 1) {
                                holder.shopRv!!.setType(false)
                            }
                        }
                        /*if(lastPosition==layoutManager.itemCount -1) {
                            holder.shopRv!!.setType(false)
                        }*/
                    }
                })

                if (centerBannerList.size > 0) {
                    val centerBanner = centerBannerList[0]
                    loadImag(holder.bannerImag!!, centerBanner.imgPath, R.drawable.lifereferrals_bannenr, 5)
                }
                val overLayCardLayoutManager = OverLayCardLayoutManager()
                CardConfig.initConfig(holder.itemView.context, 4, 0.05f, 10f)
                holder.cardRv!!.layoutManager = overLayCardLayoutManager
                mShopAdaptper = MyShopCardAdapter()
                holder.cardRv!!.adapter = mShopAdaptper
                val callback = TanTanCallback(holder.cardRv!!, mShopAdaptper!!, mCardList)
                val itemTouchHelper = ItemTouchHelper(callback)
                itemTouchHelper.attachToRecyclerView(holder.cardRv)

                val overLayCardLayoutManager1 = OverLayCardLayoutManager()
                holder.cardRv1!!.layoutManager = overLayCardLayoutManager1
                mShopAdaptper1 = MyShopCardAdapter1()
                holder.cardRv1!!.adapter = mShopAdaptper1
                val callback1 = TanTanCallback(holder.cardRv1!!, mShopAdaptper1!!, mCardList1)
                val itemTouchHelper1 = ItemTouchHelper(callback1)
                itemTouchHelper1.attachToRecyclerView(holder.cardRv1)

                holder.couponRv!!.layoutManager = LinearLayoutManager(holder.itemView.context, LinearLayoutManager.HORIZONTAL, false)
                myCouponAdapter = MyCouponAdapter()
                holder.couponRv!!.adapter = myCouponAdapter
                holder.couponRv!!.setScrollSpeed(1)
                holder.couponRv!!.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                        val layoutManager = recyclerView.layoutManager as LinearLayoutManager
                        val firstPosition = layoutManager.findFirstCompletelyVisibleItemPosition()
                        if (firstPosition == 0) {
                            holder.couponRv!!.setType(false)
                        }
                        var lastPosition = layoutManager.findLastCompletelyVisibleItemPosition()
                        if (lastPosition == layoutManager.itemCount - 1) {
                            holder.couponRv!!.setType(false)
                        }
                    }
                })
            } else if (holder is MyHolder) {
                if (shopList.size > 0) {
                    val lifeCardBean = shopList[position - 1]
                    holder.tvName.text = lifeCardBean.shopName ?: ""
                    holder.tvClassName.text = lifeCardBean.className ?: ""
                    holder.tvJL.text = ""
                    loadImag(holder.imageLogo, lifeCardBean.logo
                            ?: "", R.drawable.shop_logo_error, 50)
                    val imagUrl = if (lifeCardBean.images != null && lifeCardBean.images.isNotEmpty()) {
                        val imags = lifeCardBean.images.split(",")
                        imags[0]
                    } else {
                        lifeCardBean.images ?: ""
                    }
                    loadImag(holder.imageView, imagUrl, R.drawable.lifereferrals_bannenr, 5)
                    if (lifeCardBean.activity != null) {
                        holder.tvTitle.text = Html.fromHtml("<font color=\"#9000FC\">#热门活动#</font>" + lifeCardBean.activity!!.introduct)
                    }
                    if (lifeCardBean.maxReduceMoney != null && lifeCardBean.maxReduceMoney!! > 0) {
                        holder.tvPrice.text = "￥" + lifeCardBean.maxReduceMoney
                        holder.tvPrice.visibility = View.VISIBLE
                    } else {
                        holder.tvPrice.visibility = View.GONE
                    }
                    if (lifeCardBean.minDiscount != null && lifeCardBean.minDiscount!! > 0) {
                        holder.tvZK.text = decimalFormat.format(lifeCardBean.minDiscount!! / 10) + "折"
                        holder.tvZK.visibility = View.VISIBLE
                    } else {
                        holder.tvZK.visibility = View.GONE
                    }
                    if (lifeCardBean.giftNum != null && lifeCardBean.giftNum!! > 0) {
                        holder.tvFlName.visibility = View.VISIBLE
                    } else {
                        holder.tvFlName.visibility = View.GONE
                    }
                    holder.itemView.setOnClickListener {
                        setShopIntent(lifeCardBean.id)
                    }
                }
            }
        }

        override fun getItemViewType(position: Int): Int {
            return if (position == 0) {
                TYPETOP
            } else {
                TYPEITM
            }
        }
    }

    private inner class MyTopHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var shopRv: AutoVerticalRollRecyclerView? = null
        var cardRv: RecyclerView? = null
        var cardRv1: RecyclerView? = null
        var couponRv: AutoVerticalRollRecyclerView? = null
        var bannerImag: ImageView? = null

        init {
            bannerView = itemView.lifereferrals_banner
            shopRv = itemView.lifereferrals_shoprv
            cardRv = itemView.lifereferrals_cardrv
            cardRv1 = itemView.lifereferrals_cardrv1
            couponRv = itemView.lifereferrals_couponrv
            bannerImag = itemView.lifereferrals_centerbanner
        }
    }

    private inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var imageView = itemView.lifereferrals_rvitem_imag
        var imageLogo = itemView.lifereferrals_rvitem_logo
        var tvName = itemView.lifereferrals_rvitem_shopname
        var tvTitle = itemView.lifereferrals_rvitem_title
        var tvZK = itemView.lifereferrals_rvitem_zk
        var tvClassName = itemView.lifereferrals_rvitem_classname
        var tvJL = itemView.lifereferrals_rvitem_jl
        var tvPrice = itemView.lifereferrals_rvitem_price
        var tvFlName = itemView.lifereferrals_rvitem_flname
    }

    private inner class MyShopAdapter : RecyclerView.Adapter<MyShopHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyShopHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.lifereferrals_shoprvitm, parent, false)
            return MyShopHolder(view)
        }

        override fun getItemCount(): Int {
            return classList.size
        }

        override fun onBindViewHolder(holder: MyShopHolder, position: Int) {
            if (classList.size > 0) {
                if (position == 1) {
                    holder.view!!.visibility = View.VISIBLE
                } else {
                    holder.view!!.visibility = View.GONE
                }
                val classify = classList[position]
                loadImag(holder.imageView!!, classify.picUrl, R.drawable.ic_good_place_holder, 10)
                holder.itemView.setOnClickListener {
                    val intent = Intent(activity, LifeClassListActivity::class.java)
                    intent.putExtra("ClassID", classify.id.toString())
                    intent.putExtra("ClassName", classify.name)
                    startActivity(intent)
                }
            }
        }
    }

    private inner class MyShopHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var imageView: ImageView? = null
        var view: View? = null

        init {
            imageView = itemView.lifereferrals_shop_img
            view = itemView.lifereferrals_shopitm_zw
        }
    }

    private inner class MyShopCardAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyShopCardHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.recylerview_item, parent, false)
            return MyShopCardHolder(view)
        }

        override fun getItemCount(): Int {
            return mCardList.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (mCardList.size > 0) {
                if (holder is MyShopCardHolder) {
                    val lifeCardBean = mCardList[position] as LifeShopRecord
                    holder.tvName.text = lifeCardBean.shopName ?: ""
                    holder.tvAddress.text = lifeCardBean.address ?: ""
                    loadImag(holder.imageLogo, lifeCardBean.logo
                            ?: "", R.drawable.shop_logo_error, 50)
                    val imagUrl = if (lifeCardBean.images != null && lifeCardBean.images.isNotEmpty()) {
                        val imags = lifeCardBean.images.split(",")
                        imags[0]
                    } else {
                        lifeCardBean.images ?: ""
                    }
                    loadImag(holder.imageView, imagUrl, R.drawable.lifereferrals_bannenr, 5)
                    if (lifeCardBean.activity != null) {
                        holder.tvTitle.text = Html.fromHtml("<font color=\"#9000FC\">#热门活动#</font>" + lifeCardBean.activity!!.introduct)
                    }
                    holder.itemView.setOnClickListener {
                        setShopIntent(lifeCardBean.id)
                    }
                }
            }
        }

    }

    private inner class MyShopCardHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView = itemView.lifeshop_rvitm_imag
        val imageLogo = itemView.lifeshop_rvitm_logo
        val tvName = itemView.lifeshop_rvitm_name
        val tvTitle = itemView.lifeshop_rvitm_title
        val tvAddress = itemView.lifeshop_rvitm_address
    }

    private inner class MyShopCardAdapter1 : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyShopCardHolder1 {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.recylerview_item, parent, false)
            return MyShopCardHolder1(view)
        }

        override fun getItemCount(): Int {
            return mCardList1.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (mCardList1.size > 0) {
                if (holder is MyShopCardHolder1) {
                    val lifeCardBean = mCardList1[position] as LifeShopRecord
                    holder.tvName.text = lifeCardBean.shopName ?: ""
                    holder.imagTL.setImageResource(R.drawable.life_shop_tl1)
                    holder.tvAddress.text = lifeCardBean.address ?: ""
                    loadImag(holder.imageLogo, lifeCardBean.logo
                            ?: "", R.drawable.shop_logo_error, 50)
                    val imagUrl = if (lifeCardBean.images != null && lifeCardBean.images.isNotEmpty()) {
                        val imags = lifeCardBean.images.split(",")
                        imags[0]
                    } else {
                        lifeCardBean.images ?: ""
                    }
                    loadImag(holder.imageView, imagUrl, R.drawable.lifereferrals_bannenr, 5)
                    if (lifeCardBean.activity != null) {
                        holder.tvTitle.text = Html.fromHtml("<font color=\"#9000FC\">#热门活动#</font>" + lifeCardBean.activity.introduct)
                    }
                    holder.itemView.setOnClickListener {
                        setShopIntent(lifeCardBean.id)
                    }
                }
            }
        }

    }

    private inner class MyShopCardHolder1(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView = itemView.lifeshop_rvitm_imag
        val imageLogo = itemView.lifeshop_rvitm_logo
        val imagTL = itemView.lifeshop_rvitm_imgtl
        val tvName = itemView.lifeshop_rvitm_name
        val tvTitle = itemView.lifeshop_rvitm_title
        val tvAddress = itemView.lifeshop_rvitm_address
    }

    private inner class MyCouponAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.lifereferrals_couponrvitm, parent, false)
            return MyCouponHolder(view)
        }

        override fun getItemCount(): Int {
            return cardList.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (cardList.size > 0) {
                if (holder is MyCouponHolder) {
                    val lifeWXCardBean = cardList[position]
                    when (lifeWXCardBean.cardType) {
                        "CASH" -> { // 代金券类型
                            holder.tvRMB!!.visibility = View.VISIBLE
                            holder.tvSm!!.text = "满" + lifeWXCardBean.leastMoneyToUse + "可用"
                            holder.tvPrice!!.text = lifeWXCardBean.reduceMoney ?: ""
                        }
                        "DISCOUNT" -> { //折扣券类型
                            holder.tvRMB!!.visibility = View.GONE
                            holder.tvSm!!.text = "折扣券"
                            val zk = decimalFormat.format((lifeWXCardBean.discount / 10)) + "折"
                            holder.tvPrice!!.text = zk
                        }
                        "GIFT" -> { //兑换券类型
                            holder.tvRMB!!.visibility = View.GONE
                            holder.tvPrice!!.text = "兑换券"
                            holder.tvSm!!.text = "进店可兑换礼品"
                        }
                        else -> { //其他
                            holder.tvSm!!.text = "满" + lifeWXCardBean.leastMoneyToUse + "可用"
                            holder.tvPrice!!.text = lifeWXCardBean.reduceMoney ?: ""
                        }
                    }
                    if (lifeWXCardBean.wxUserCard != null) {
                        holder.imgYLQ!!.visibility = View.VISIBLE
                        holder.tvName!!.text = "去使用"
                        holder.tvName!!.background = resources.getDrawable(R.drawable.shape_coupon_but_y)
                        holder.tvRMB!!.setTextColor(resources.getColor(R.color.golden_e3))
                        holder.tvPrice!!.setTextColor(resources.getColor(R.color.golden_e3))
                        holder.tvSm!!.setTextColor(resources.getColor(R.color.golden_e3))
                    } else {
                        holder.imgYLQ!!.visibility = View.INVISIBLE
                        holder.tvName!!.text = lifeWXCardBean.shopName ?: ""
                        holder.tvName!!.background = resources.getDrawable(R.drawable.shape_cc_e0_30)
                        holder.tvRMB!!.setTextColor(resources.getColor(R.color.golden_96))
                        holder.tvPrice!!.setTextColor(resources.getColor(R.color.golden_96))
                        holder.tvSm!!.setTextColor(resources.getColor(R.color.golden_96))
                    }
                    if (position == 0) {
                        holder.view!!.visibility = View.VISIBLE
                    } else {
                        holder.view!!.visibility = View.GONE
                    }
                    holder.itemView.setOnClickListener {
                        if (lifeWXCardBean.wxUserCard == null) {
                            getWXExt(lifeWXCardBean.cardId, lifeWXCardBean.wxId)
                        } else {
                            setShopIntent(lifeWXCardBean.id)
                        }
                    }
                }
            }
        }
    }

    private inner class MyCouponHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var view: View? = null
        var tvPrice: TextView? = null
        var tvSm: TextView? = null
        var tvName: TextView? = null
        var imgYLQ: ImageView? = null
        var tvRMB: TextView? = null

        init {
            view = itemView.lifereferrals_couponitmzw
            tvPrice = itemView.lifereferrals_couponitm_price
            tvSm = itemView.lifereferrals_couponitm_gz
            tvName = itemView.lifereferrals_couponitm_name
            imgYLQ = itemView.lifereferrals_couponitm_ylq
            tvRMB = itemView.lifereferrals_couponitm_rmb
        }
    }

    inner class GlideImageLoader : ImageLoader() {
        override fun displayImage(context: Context, path: Any, imageView: ImageView) {
            val lifeTopBanner = path as LifeTopBanner
            val url = lifeTopBanner.imgPath!!
            val imgUrl = if (url.startsWith("//")) {
                TCP + url
            } else if (url.startsWith("http")) {
                url
            } else {
                REMOTE_IMAGE_URL + url
            }
            Glide.with(context).asBitmap()
                    .apply(RequestOptions().transforms(CenterCrop(), RoundedCorners(10)).error(R.drawable.ic_app_place_holder2))
                    .load(imgUrl)
                    .into(imageView)
        }
    }
}